/*
 * @Descripttion: Descripttion
 * @version: 1.0.0
 * @Author: kaka
 * @Date: 2020-03-04 15:16:05
 * @LastEditors: kaka
 * @LastEditTime: 2020-03-10 11:42:37
 */
(function() {
    let nowpath, isImg = [],
        wsList = [];
    //获取ip列表
    const getIpList = function() {
            for (let i = 0; i < ipList.length; i++) { //循环ip列表控制所有的机器
                isImg.push(false);
                wsList.push(new WebSocket(`ws://${ipList[i]}:9090/jsonrpc?`))
                wsList[i].onopen = function(e) {
                    if (i === 0) {
                        wsList[0].send(JSON.stringify({
                            "jsonrpc": "2.0",
                            "method": "Files.GetDirectory",
                            "id": "findFiles",
                            "params": {
                                "directory": "/storage/emulated/0/kodi_file/",
                                "media": "files",
                                "properties": ["title", "file", "mimetype", "thumbnail", "dateadded"],
                                "sort": { "method": "none", "order": "ascending" }
                            }
                        }))
                    }
                }
                wsList[i].onerror = function(e) {
                    let div = document.createElement('div');
                    div.innerText = `${ipList[i]}未开启`;
                    document.getElementById('txt').appendChild(div);
                }
                findFiles(i);
            }
        }
        //根据路径获取文件夹列表或者视频列表，如果是文件夹列表，则更新下拉选项，如果是视频列表，则插入播放
    const findFiles = function(i) {
        wsList[i].onmessage = function(e) {
            let res = JSON.parse(e.data)
            if (i === 0 && res.id && res.id === 'findFiles') {
                updateDom(res.result.files)
            }
            if (res.id && res.id === "checkFileType") {
                console.log(i, res)
                if (res.result.files[0].file.split('.').pop() !== 'jpg') {
                    if (isImg[i]) { back(i) }
                    isImg[i] = false;
                    showVideo(i)
                } else {
                    isImg[i] = true;
                    showImg(i, res.result.files[0].file)
                }
            }
        }
    }
    const updateDom = function(data) {
        let html = '<div class="item"><select name="ip" id="ip">';
        for (let j = 0; j < data.length; j++) {
            let path = data[j].file.split('/')[5];
            html += `<option value="${path}">${path}</option>`;
        }
        html += '</select><button id="play">播放列表</button></div>';
        let con = document.getElementById('con');
        con.innerHTML = html;
        nowpath = data[0].file;
        let select = document.getElementById('ip');
        let play = document.getElementById('play');
        select.onchange = function(e) {
            nowpath = `/storage/emulated/0/kodi_file/${e.target.value}/`;
        }
        let that = this;
        play.addEventListener('click', function() {
            for (let i = 0; i < wsList.length; i++) {
                checkFileType(i)
            }
        })
    }
    const checkFileType = function(i) {
        wsList[i].send(JSON.stringify({
            "jsonrpc": "2.0",
            "method": "Files.GetDirectory",
            "id": "checkFileType",
            "params": {
                "directory": nowpath,
                "media": "files",
                "properties": ["title", "file", "mimetype", "thumbnail", "dateadded"],
                "sort": { "method": "none", "order": "ascending" }
            }
        }))
    }
    const showVideo = function(i) { // 展示视频
        wsList[i].send(
            JSON.stringify([{ "jsonrpc": "2.0", "method": "Player.Open", "params": { "item": { "directory": "/storage/emulated/0/kodi_file/BWG/" }, "options": { "repeat": "all" } }, "id": "showVideo" }]))
    }
    const showImg = function(i, path) { //展示图片
        wsList[i].send(JSON.stringify({ "jsonrpc": "2.0", "method": "Player.Open", "params": { "item": { "file": path } }, "id": 1582725439528 }))
    }
    const back = function(i) { //返回上一级
        wsList[i].send(JSON.stringify({ "jsonrpc": "2.0", "method": "Input.Back", "id": "1" }))
    }
    getIpList();
})()